package com.houger.ddd.seckill.infra.datamanager;

import com.houger.ddd.datamanager.manager.aop.DataCache;
import com.houger.ddd.datamanager.manager.aop.DataCacheEvict;
import com.houger.ddd.datamanager.manager.aop.InventoryReference;
import com.houger.ddd.datamanager.utils.CacheType;
import com.houger.ddd.seckill.infra.entity.ActivityRewardItem;
import com.houger.ddd.seckill.repository.ActivityRewardItemRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActivityRewardItemDataManager {

    @Resource
    ActivityRewardItemRepository activityRewardItemRepository;

    @DataCache(group = "activity-item",key = "('templateId=').concat(#templateId)",inventoryReference = {
            @InventoryReference(field = "invUsed", ref = ActivityRewardItemInventoryOperator.class)
    },type = {CacheType.LOCAL, CacheType.REDIS},localExpire = 60, redisExpire = 1 * 60)
    public List<ActivityRewardItem> queryItemListByTemplateId(Long templateId) {
        return this.activityRewardItemRepository.findByTemplateId(templateId).orElseThrow();
    }

    @DataCache(group = "activity-item",key = "('id=').concat(#id)",inventoryReference = {
            @InventoryReference(field = "invUsed", ref = ActivityRewardItemInventoryOperator.class)
    },type = {CacheType.LOCAL, CacheType.REDIS},localExpire = 60, redisExpire = 1 * 60)
    public ActivityRewardItem queryById(Long id) {
        return this.queryByIdFromDb(id);
    }

    public ActivityRewardItem queryByIdFromDb(Long id) {
        return this.activityRewardItemRepository.findById(id).orElseThrow();
    }

    @DataCacheEvict(group = "activity-item",key = "('templateId=').concat(#templateId)",type = {CacheType.LOCAL, CacheType.REDIS})
    public void evict(Long templateId){

    }

    @DataCacheEvict(group = "activity-item",key = "('id=').concat(#id)",type = {CacheType.LOCAL, CacheType.REDIS})
    public void evictById(Long id){

    }

}
